<?php

class MenuController extends Controller
{
    public $layout = '//layouts/main';
    public $defaultAction = 'manage';

    public function actionManage()
    {
        $model = new Menu('search');
        if (isset($_GET['Menu'])) {
            $model->attributes = $_GET['Menu'];
        }
        if (isset($_GET['searchKey'])) {
            $model->searchKey = $_GET['searchKey'];
        }
        $this->render('manage', array(
            'model' => $model,
        ));
    }

    public function actionCreate()
    {
        $model = new Menu();
        if (isset($_POST['Menu'])) {
            $model->attributes = $_POST['Menu'];
            if ($model->save()) {
                $this->redirect(array($this->defaultAction));
            }
        }
        $this->render('model', array(
            'model' => $model,
        ));
    }

    public function actionUpdate()
    {
        if (isset($_GET['id'])) {
            $model = Menu::model()->findByPk($_GET['id']);
            if ($model) {
                if (isset($_POST['Menu'])) {
                    $model->attributes = $_POST['Menu'];
                    if ($model->save()) {
                        $this->redirect(array($this->defaultAction));
                    }
                }
                $this->render('model', array(
                    'model' => $model,
                ));
            } else {
                $this->redirect(array($this->defaultAction));
            }
        } else {
            $this->redirect(array($this->defaultAction));
        }
    }

    public function actionDelete($id)
    {
        if (Yii::app()->request->isPostRequest) {
            $model = Menu::model()->with('subs')->findByPk($id);
            if ($model) {
                if (empty($model->subs)) {
                    if ($model->delete()) {
                        echo 'Success';
                    } else {
                        echo 'Error:DB Delete Failure!';
                    }
                } else {
                    echo 'Error:Exist SubMenu of The Menu';
                }
            } else {
                echo 'Error:Object is not Exist!';
            }
        } else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }


    public function colRevise($data, $row, $c)
    {
        $name = $c->name;
        switch ($name) {
            case 'pid':
                if ($data->$name == 0) {
                    return '--';
                }
                $parent = Menu::model()->findByPk($data->$name);
                if ($parent) {
                    return $parent->name;
                } else {
                    return 'NoExist';
                }
                break;
            case 'type':
                if ($data->pid == 0) {
                    return $data->$name;
                } else {
                    $parent = Menu::model()->findByPk($data->pid);
                    if ($parent) {
                        return $parent->type . '<i>(' . $data->$name . ')</i>';
                    } else {
                        return $data->$name;
                    }
                }
                break;
            default:
                return $data->$name;
        }
    }
    // Uncomment the following methods and override them if needed
    /*
    public function filters()
    {
        // return the filter configuration for this controller, e.g.:
        return array(
            'inlineFilterName',
            array(
                'class'=>'path.to.FilterClass',
                'propertyName'=>'propertyValue',
            ),
        );
    }

    public function actions()
    {
        // return external action classes, e.g.:
        return array(
            'action1'=>'path.to.ActionClass',
            'action2'=>array(
                'class'=>'path.to.AnotherActionClass',
                'propertyName'=>'propertyValue',
            ),
        );
    }
    */
}